/*
 * JFTermAppletSingle.java
 *
 * This is the unsigned version for connecting to one predefined site (see applet-single.html)
 *
 * Created on August 11, 2007, 1:24 PM
 *
 * @author  pquiring
 */

import javaforce.*;
import java.awt.*;
import java.awt.event.*;

public class TermAppletSingle extends javax.swing.JApplet implements KeyEventDispatcher {

  public boolean dispatchKeyEvent(KeyEvent e) {
    System.out.println("KeyEvent:" + e);
    if ((e.getSource() instanceof Buffer) && (e.getKeyCode() == e.VK_TAB)) {
      return true;  //do not pass on
    }
    return false;  //pass on as normal
  }

  /** Initializes the applet JFTermAppletSingle */
  public void init() {
    try {
      java.awt.EventQueue.invokeAndWait(new Runnable() {
        public void run() {
          initComponents();
          initBuffer();
        }
      });
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    //disable TAB processing for Buffer
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);

  }

  /** This method is called from within the init() method to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scroll = new javax.swing.JScrollPane();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scroll;
    // End of variables declaration//GEN-END:variables

  public Buffer buffer;

  public void initBuffer() {
    buffer = new Buffer();

    SiteDetails sd = new SiteDetails();
    sd.name = "";

    String cols = getParameter("cols");
    if ((cols == null) || (cols.length() == 0)) cols = "80";
    Settings.settings.cols = JF.atoi(cols);
    sd.sx = Settings.settings.cols;
    String rows = getParameter("rows");
    if ((rows == null) || (rows.length() == 0)) rows = "24";
    Settings.settings.rows = JF.atoi(rows);
    sd.sy = Settings.settings.rows;
    String scrollback = getParameter("scrollback");
    if ((scrollback == null) || (scrollback.length() == 0)) scrollback = "3000";
    Settings.settings.scrollBack = JF.atoi(scrollback);
    sd.host = getParameter("host");
    if ((sd.host == null) || (sd.host.length() == 0)) sd.host = "localhost";
    sd.protocol = getParameter("protocol");
    if ((sd.protocol == null) || (sd.protocol.length() == 0)) sd.protocol = "telnet";
    sd.port = getParameter("port");
    if ((sd.port == null) || (sd.port.length() == 0)) {
      if (sd.protocol.equalsIgnoreCase("telnet")) sd.port = "23";
      else if (sd.protocol.equalsIgnoreCase("ssh")) sd.port = "22";
      else if (sd.protocol.equalsIgnoreCase("ssl")) sd.port = "443";
      else sd.port = "23";
    }
    sd.username = getParameter("username");
    if ((sd.username == null) || (sd.username.length() == 0)) sd.username = "root";
    sd.password = getParameter("password");
    if ((sd.password == null) || (sd.password.length() == 0)) sd.password = "password";

    buffer.applet = true;
    buffer.setSiteDetails(sd);
    scroll.setViewportView(buffer);
    scroll.revalidate();
    buffer.requestFocus();
    buffer.repaint(true);
  }
}
